package com.online.findagentserver.service;

import com.alibaba.fastjson.JSON;
import com.online.findagentserver.domain.WeChatUserInfo;
import com.online.findagentserver.domain.WeChatUserInfoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: liuqing
 * @Despriction:
 * @Date:Created in 2018/4/24 17:02
 * @Modify By:
 */
@Repository
public class RedisService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    final static String WAIT = "-WAIT";
    final static String AGENT = "-AGENT";
    final static String COMPANY = "-COMPANY";
    final static String DOING = "-DOING";

    public String findWaitSessionAgent(String eId, String openId) {
        Object obj = stringRedisTemplate.opsForHash().get(eId+WAIT, openId);
        if (!StringUtils.isEmpty(obj)) {
            return (String) obj;
        } else {
            return null;
        }
    }

    public String findSessionAgent(String eId, String openId) {
        Object obj = stringRedisTemplate.opsForHash().get(eId+DOING, openId);
        if (!StringUtils.isEmpty(obj)) {
            return (String) obj;
        } else {
            return null;
        }
    }

    public void deleteChatSession(String key, String... key2) {
        stringRedisTemplate.opsForHash().delete(key + AGENT, key2);
    }

    public long deleteMessage(Collection<String> key) {
        return stringRedisTemplate.delete(key);
    }

    public void saveWaitCompanyOpenId(String cId, String openId, String sessionId) {
        stringRedisTemplate.opsForHash().put(cId + WAIT, openId, sessionId);
    }

    public void removeWaitCompanyOpenId(String cId, String openId) {
        stringRedisTemplate.opsForHash().delete(cId + WAIT, openId);
    }

    public String getWaitCompanyOpenId(String cId, String openId) {
        return (String) stringRedisTemplate.opsForHash().get(cId + WAIT, openId);
    }

    public Set<Object> getWaitCompanyOpenId(String cId) {
        return stringRedisTemplate.opsForHash().keys(cId + WAIT);
    }

    public Set<String> findAIds(String eId) {
        return stringRedisTemplate.opsForSet().members(eId+COMPANY);
    }

    public long getSessionCount(String aId) {
        return stringRedisTemplate.opsForHash().size(aId + AGENT);
    }

    public void saveCompanyOpenId(String cId, String openId, String str2) {
        stringRedisTemplate.opsForHash().put(cId+DOING, openId, str2);
    }

    public void saveSessionOfAgentOpenId(String aId, String openId, String str2) {
        stringRedisTemplate.opsForHash().put(aId + AGENT, openId, str2);
    }



    //-----------------------以下是从sessionmanager 服务迁移过来-------------------------------//

    public Map<Object, Object> getAgentSessions(String aId) {
        return stringRedisTemplate.opsForHash().entries(aId + AGENT);
    }

    public void addSet(String keyOfCId, String aId) {
        stringRedisTemplate.opsForSet().add(keyOfCId, aId);
    }

    public void delSet(String keyOfCId, String aId) {
        stringRedisTemplate.opsForSet().remove(keyOfCId, aId);
    }

    public void add(String key, String str, Long time) {
        stringRedisTemplate.opsForValue().set(key, str, time, TimeUnit.MINUTES);
    }

    public void expire(String key, Long time) {
        stringRedisTemplate.expire(key, time, TimeUnit.MINUTES);
    }

    public void add(String key, String str) {
        stringRedisTemplate.opsForValue().set(key, str);
    }

    public String getStr(String key) {
        String source = stringRedisTemplate.opsForValue().get(key);
        System.out.println("source:" + source);
        if (!StringUtils.isEmpty(source)) {
            return source;
        }
        return null;
    }

    public List<String> getMessageList(String sessionId) {
        List<String> msgList = stringRedisTemplate.opsForList().range(sessionId, 0, -1);
        if (msgList != null) {
            stringRedisTemplate.opsForList().trim(sessionId, msgList.size(), -1);
        }
        return msgList;
    }

    public WeChatUserInfo getWeChatUserInfo(String key) {
        String source = stringRedisTemplate.opsForValue().get(key);
        System.out.println("source:" + source);
        if (!StringUtils.isEmpty(source)) {
            return JSON.parseObject(source, WeChatUserInfo.class);
        }
        return null;
    }

    public List<WeChatUserInfoSession> getWeChatUserInfo(Map<Object, Object> agentSessionsMap) {
        Collection<String> collection = new ArrayList<>();
        Iterator<Object> it = agentSessionsMap.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            collection.add(key);
        }

        List<String> weChatUserList = stringRedisTemplate.opsForValue().multiGet(collection);
        if (weChatUserList != null && weChatUserList.size() != 0) {
            List<WeChatUserInfoSession> wcuis = new ArrayList<>();
            for (int i = 0; i < weChatUserList.size(); i++) {
                WeChatUserInfo weChatUserInfo = JSON.parseObject(weChatUserList.get(i), WeChatUserInfo.class);
                WeChatUserInfoSession weChatUserInfoSession = new WeChatUserInfoSession(weChatUserInfo.getOpenId(),
                        weChatUserInfo.getName(),
                        weChatUserInfo.getAddress(),
                        weChatUserInfo.geteId(),
                        weChatUserInfo.getSex(),
                        Integer.parseInt((String) agentSessionsMap.get(weChatUserInfo.getOpenId())));
                wcuis.add(weChatUserInfoSession);
            }
            return wcuis;
        }

        return null;
    }


    public boolean delete(String key) {
        return stringRedisTemplate.delete(key);
    }

}
